package com.esisa.beans;

import java.io.RandomAccessFile;
import java.util.Vector;


public class ListeSCORM {
	
private Vector<Etudiant> list;
	
	public ListeSCORM(){
		list=new Vector<Etudiant>();
	}
	
	public void insert(Etudiant d){
		list.add(d);
	}
	public int size() {
		return list.size();
	}
	public Etudiant getAt(int i){
		return list.get(i);
	}
	
	public void add(Etudiant d){
		list.add(d);
	}
	
	public void save(String fileName){
		try {
			RandomAccessFile f=new RandomAccessFile(fileName, "rw");
			f.setLength(0);
			//f.seek(f.length());
			for(Etudiant d: list){
				//System.out.println(d);
				f.writeBytes(d + "\n");
			}
			f.close();
			
		} catch (Exception e) {
			System.out.println("Erreur :" +e.getMessage() );
		}
	}
	public void print(){
		for (int i = 0; i < this.list.size(); i++) {
			System.out.println(this.list.get(i).toString());
		}
		
	}
	
	
	public void load(String fileName){
		
		try {
			
			RandomAccessFile f=new RandomAccessFile(fileName, "r");
			String line=f.readLine();
			//System.out.println(line);
			
			while(line!=null && !line.trim().equals("\n")){
				String s,res = "";
				StringBuffer chemin=new StringBuffer(line);
				//System.out.println(chemin);
				do{
					line=f.readLine();
					if(line.trim().equals("}")==false){
						//System.out.println(line);
						res+=line.split(":")[1];
						res+="&";
					}
					//System.out.println(line);
				}while (line.trim().equals("}")==false);
					
				
				chemin.delete(chemin.indexOf(" >{"),chemin.length());
				chemin.delete(0,11);
				res+=chemin.toString();
				//System.out.println(res);
				this.list.add(new Etudiant(res));
				//System.out.println("ici2");
				line=f.readLine();
				line=f.readLine();
				//System.out.println(line+"goo");
			}
			
			f.close();
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Erreur :" +e.getMessage() );
		}
	}
	
	

	public Vector<Etudiant> getList() {
		return list;
	}
	
	
	public Object[][] getMatriceDoc(){
		Object tab[][] = new Object[this.list.size()][8];
		for (int i = 0; i < this.list.size(); i++) {
			tab[i][0] = this.list.get(i).getNom();
			tab[i][1] = this.list.get(i).getPrenom();
			tab[i][2] = this.list.get(i).getChemin();
			tab[i][3] = this.list.get(i).getAge();
			tab[i][4] = this.list.get(i).getAbsence();
			tab[i][5] = this.list.get(i).getPresence();
			tab[i][6] = this.list.get(i).getRetard();
			tab[i][7] = this.list.get(i).getRemarque();
			
		}
		
		return tab;
	}

	public void modify(String chemin,Etudiant d){
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getChemin().equals(chemin)) {
				list.set(i, d);
			}
		}
		save("resources/doc.txt");
	}
}
